﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Add_F5TurnOver : Form
    {        
        public frm_Add_F5TurnOver()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);            
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }        
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string err = null;
            int month = 0;
            int year = 0;
            float turnover = 0;

            if (string.IsNullOrEmpty(this.monthText.Text))
            {
                err = "Please enter Month.";
            }
            else
            {
                if (this.monthText.Text.Trim().Length != 6)
                {
                    err = "Invalid Month.";
                }
                else
                {
                    if (Int32.TryParse(this.monthText.Text.Trim().Substring(0, 4), out year))
                    {
                        if ((year < 1980) || (year > 2050))
                        {
                            err = "Invalid Month.";
                        }
                    }
                    else
                    {
                        err = "Invalid Month.";
                    }

                    if (Int32.TryParse(this.monthText.Text.Trim().Substring(4, 2), out month))
                    {
                        if ((month < 1) || (month > 12))
                        {
                            err = "Invalid Month.";
                        }
                    }
                    else
                    {
                        err = "Invalid Month.";
                    }
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.turnoverValue.Text.Trim()))
                {
                    err = "Please enter Turn Over.";
                }
                else
                {
                    if (!float.TryParse(this.turnoverValue.Text.Trim(), out turnover))
                    {
                        err = "Invalid Turn Over";
                    }
                    else
                    {
                        if (turnover <= 0)
                        {
                            err = "Turn Over must be greater than 0.";
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(err))
            {                
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "INSERT INTO [231_TurnOver] (MonthOfYear, TurnOver)"
                        + " VALUES ('" + this.monthText.Text.Trim() + "', " + turnover + ")";
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    MessageBox.Show("Add Turn Over successfully.");
                }
                catch
                {
                    MessageBox.Show("Cannnot add Turn Over.");
                }
                finally
                {
                    myConnection.Close();
                }
            }
            else
            {
                MessageBox.Show(err);
            }
        }
  
    }
}
